package com.wz.excelimport.service.impl;

import com.wz.excelimport.entity.FuEntity;
import com.wz.excelimport.entity.UserEntity;
import com.wz.excelimport.service.ExcelImportService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class ExcelImportServiceImpl implements ExcelImportService {



    @Override
    public List<FuEntity> batchFuImport(String fileName, MultipartFile file) throws Exception {

        List<FuEntity> fuEntities = new ArrayList<FuEntity>();
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            throw new Exception("上传文件格式不正确");
        }
        boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        Sheet sheet = wb.getSheetAt(0);
        if (sheet != null) {
            FuEntity fuEntity;
            for (int r = 1; r <= sheet.getLastRowNum(); r++) {
                Row row = sheet.getRow(r);
                if (row == null) {
                    continue;
                }


                fuEntity = new FuEntity();


                String fu = row.getCell(4).getStringCellValue();
                fu = fu.replaceAll("\t","").trim();
                if (fu == null || fu.isEmpty()) {
                    throw new Exception("导入失败(第" + (r + 1) + "行,题干未填写)");
                }

                if (fu.equals("神农福")) {



                    String openId = row.getCell(1).getStringCellValue();
                    openId = openId.replaceAll("\t","").trim();

                    if (openId == null || openId.isEmpty()) {
                        throw new Exception("导入失败(第" + (r + 1) + "行,题干未填写)");
                    }





                    fuEntity.setOpenId(openId);
                    fuEntity.setFuName(fu);
//
//                singerChoiceqst.setChoiceqstInfo(choiceqstInfo);
//                singerChoiceqst.setChoiceqstOp(JsonUtils.objectToJson(opList));
//                singerChoiceqst.setChoiceqstAnsw(choiceqstAnsw);
//                singerChoiceqst.setChoiceqstAnalysis(choiceqstAnalysis);
//                singerChoiceqstList.add(singerChoiceqst);
                    fuEntities.add(fuEntity);
                }


            }
        }

//        for (SingerChoiceqst qstResord : singerChoiceqstList) {
//
//            boolean b = singerChoiceqstService.addSingerQst(qstResord);
//
//        }
        return fuEntities;
    }



    @Override
    public List<UserEntity> batchUserImport(String fileName, MultipartFile file)  {

        try {
            List<UserEntity> userEntities = new ArrayList<UserEntity>();
            if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
                throw new Exception("上传文件格式不正确");
            }
            boolean isExcel2003 = true;
            if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
                isExcel2003 = false;
            }
            InputStream is = file.getInputStream();
            Workbook wb = null;
            if (isExcel2003) {
                wb = new HSSFWorkbook(is);
            } else {
                wb = new XSSFWorkbook(is);
            }
            Sheet sheet = wb.getSheetAt(0);
            if (sheet != null) {
                UserEntity userEntity;
                for (int r = 1; r <= sheet.getLastRowNum(); r++) {
                    Row row = sheet.getRow(r);
                    if (row == null) {
                        continue;
                    }

                    userEntity = new UserEntity();

                    String openId = row.getCell(1).getStringCellValue();
                    openId = openId.replaceAll("\t","").trim();

                    if (openId == null || openId.isEmpty()) {
                        throw new Exception("导入失败(第" + (r + 1) + "行,openid未填写)");
                    }


                    String weixinname = row.getCell(2).getStringCellValue();
                    weixinname = weixinname.replaceAll("\t","").trim();

                    if (weixinname == null || weixinname.isEmpty()) {
                        weixinname = "";
                    }

                    String username = row.getCell(3).getStringCellValue();
                    username = username.replaceAll("\t","").trim();

                    if (username == null || username.isEmpty()) {
                        username = "";
                    }

                    String phone = row.getCell(4).getStringCellValue();
                    phone = phone.replaceAll("\t","").trim();
                    if (phone == null || phone.isEmpty()) {
                        phone = "";
                    }


                    userEntity.setOpenId(openId);
                    userEntity.setWeixinname(weixinname);
                    userEntity.setUserName(username);
                    userEntity.setPhone(phone);
//
//                singerChoiceqst.setChoiceqstInfo(choiceqstInfo);
//                singerChoiceqst.setChoiceqstOp(JsonUtils.objectToJson(opList));
//                singerChoiceqst.setChoiceqstAnsw(choiceqstAnsw);
//                singerChoiceqst.setChoiceqstAnalysis(choiceqstAnalysis);
//                singerChoiceqstList.add(singerChoiceqst);
                    userEntities.add(userEntity);
                }
            }

//        for (SingerChoiceqst qstResord : singerChoiceqstList) {
//
//            boolean b = singerChoiceqstService.addSingerQst(qstResord);
//
//        }
            return userEntities;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }

}
